package universal.schema.action;

import java.util.*;

import universal.schema.model.Filters;
import universal.schema.model.Graph;
import universal.schema.model.SearchModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class step4Action extends ActionSupport {

	String finalTables[],finalCols[];
	ArrayList<Filters> finalFilters=null;
	ArrayList<String> step4Res;
	ArrayList<String> selectedTablecol=new ArrayList<String>();
	Map session=ActionContext.getContext().getSession();;
	String resultQuery="";
	String sqlQuery="";
	//String query1="";

	SearchModel obj=new SearchModel();

	public String proceedtoQuery()
	{
		finalTables=(String[])session.get("selectedTables");
		finalCols=(String[])session.get("selectedColumns");

		if(finalFilters==null)
		{
			finalFilters=new ArrayList<Filters>();
		}
		else
		{
			finalFilters.clear();
		}


		finalFilters=(ArrayList<Filters>)session.get("finalFilters");	
	
		if(finalTables.length==1){
			resultQuery=obj.singleTableQueryResults(finalCols,finalTables[0],finalFilters);
		}
		else{
			resultQuery=obj.multipleTableResults(finalCols,finalTables,finalFilters);
		}
		
		session.put("queryBefore", resultQuery);
		return SUCCESS;
	}

	public String proceedtoQueryNoWhere()
	{
		finalTables=(String[])session.get("selectedTables");
		finalCols=(String[])session.get("selectedColumns");

		if(finalTables.length==1){
			System.out.println("length of col list"+finalCols.length);
			resultQuery=obj.singleTableQueryResultsNoWhere(finalCols,finalTables[0]);
		}
		else
		{
			System.out.println("in this action");
			System.out.println("length of col list"+finalCols.length);
			resultQuery=obj.multipleTableResultsNoWhere(finalCols,finalTables);
		}
		session.put("queryBefore", resultQuery);
		
		return SUCCESS;
	}

	
	@SuppressWarnings("unchecked")
	public String addOrderBy()
	{
		System.out.println("Goin to order by modal");
		String[] array=(String[])session.get("ColsOfSelectedTables");
		System.out.println("length of selected table cols "+array.length);
		
		for(int j=0;j<array.length;j++){
			selectedTablecol.add(array[j]);	
		}
		
		 System.out.println("size of selected table column "+selectedTablecol.size());
		return SUCCESS;
	}
	
	public String addGroupBy()
	{
		System.out.println("Goin into group by modal");
		return SUCCESS;
	}
	
	public String addHaving()
	{
		System.out.println("Goin to having modal");
		return "success";
	}
	
	public String closemodal()
	{
	   System.out.println("Close of order by modal....");
	   return "close";
	}
	
	public String validatQuery() 
	{
		try{
			
			System.out.println("query from textarea" + sqlQuery);
			
			step4Res=obj.results();

			if(step4Res==null){
				addActionError("Query is incorrect.Please check the Query!!!!");
				return "error";
			}

			else
			{
				return SUCCESS;
			}
		}
		catch(Exception e){
			//e.printStackTrace();
			return "error";
		}
	}

	@SuppressWarnings("rawtypes")
	public String restartWork(){

		//INVALIDATING SESSIONS
		Map session = ActionContext.getContext().getSession();

		if (session instanceof org.apache.struts2.dispatcher.SessionMap) {
			//System.out.println("In session invalidate......");
			try {
				((org.apache.struts2.dispatcher.SessionMap) session).invalidate();
				session.clear();
			} catch (IllegalStateException e) {
				System.out.println("Exception.."+e);
			}
		}
		
		//clearing static values
		if(searchAction.checkColumns != null){
			for(int i=0;i<searchAction.checkColumns.length;i++)
			{
				searchAction.checkColumns[i]="";	
			}
			System.out.println("searchAction.checkColumns.length " + searchAction.checkColumns.length);
			searchAction.checkColumns=null;
		}

		if(searchAction.checkTable != null){
			for(int i=0;i<searchAction.checkTable.length;i++)
			{
				searchAction.checkTable[i]="";	
			}
			System.out.println("searchAction.checkTable.length "+searchAction.checkTable.length);
			searchAction.checkTable=null;
		}

		
		searchAction.nodesList.clear();
		searchAction.selectedNodesList.clear();
		searchAction.getResults.clear();
		Graph.hash_bucket.clear();
		Graph.linker_nodes.clear();
		Graph.linker_nodes_association.clear();
				
        if(getStep4Res()!=null)
        	getStep4Res().clear();
        if(!getSqlQuery().isEmpty() && getSqlQuery()!=null)
        	setSqlQuery(null);

		return SUCCESS;
	}

	/*
	 * Getters & Setters
	 */
	public String[] getFinalTables() {
		return finalTables;
	}

	public void setFinalTables(String[] finalTables) {
		this.finalTables = finalTables;
	}

	public String[] getFinalCols() {
		return finalCols;
	}

	public void setFinalCols(String[] finalCols) {
		this.finalCols = finalCols;
	}

	public ArrayList<Filters> getFinalFilters() {
		return finalFilters;
	}

	public void setFinalFilters(ArrayList<Filters> finalFilters) {
		this.finalFilters = finalFilters;
	}

	public String getResultQuery() {
		return resultQuery;
	}

	public void setResultQuery(String resultQuery) {
		this.resultQuery = resultQuery;
	}

	public String getSqlQuery() {
		return sqlQuery;
	}

	public void setSqlQuery(String sqlQuery) {
		this.sqlQuery = sqlQuery;
	}

	public ArrayList<String> getStep4Res() {
		return step4Res;
	}

	public void setStep4Res(ArrayList<String> step4Res) {
		this.step4Res = step4Res;
	}

	public ArrayList<String> getSelectedTablecol() {
		return selectedTablecol;
	}

	public void setSelectedTablecol(ArrayList<String> selectedTablecol) {
		this.selectedTablecol = selectedTablecol;
	}

	/*public String getQuery1() {
		return query1;
	}

	public void setQuery1(String query1) {
		this.query1 = query1;
	}*/



}


//can adjust only the where clause and not the columns in the step4 jsp